<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@include file="../base.jsp" %> 

<sys:genericTag4o id="menuTree4RoleConfJson" methodName="menuTree4RoleConfJson" className="com.tmpt.controller.taglib.SecurityV2Tag"/>

<sys:genericTag4o id="roleDtoList" methodName="queryA2RoleList" className="com.tmpt.controller.taglib.SecurityV2Tag"/>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>    
<link href="../css/main.css" rel="stylesheet" type="text/css" >
<link rel="stylesheet" type="text/css" href="../../js/jquery/easyui1.5/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../../js/jquery/easyui1.5/themes/icon.css">
<script type="text/javascript" src="../../js/jquery-1.8.0.min.js"></script>
<script src="../../js/jquery/easyui1.5/jquery.easyui.min.js"></script>
<script src="../../js/jquery/easyui1.5/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
$(function(){
	
	$('#tt1').treegrid({ 
		data:${menuTree4RoleConfJson},
		checkbox: true,
		rownumbers: true,
		idField: 'id',
		treeField:'text', 
		columns:[[ 
		{title:'ID',field:'id',width:30}, 
		{title:'菜单名称',field:'text',width:250}, 
		{field:'opt',title:'操作',width:250,align:'center',
			formatter:function(value,rowData,rowIndex){
				if(rowData.attributes.level == 2){
					var menu1Id = rowData.attributes.menu1Id;
					var menu2Id = rowData.attributes.menu2Id;
					var radioName = "radio_"+menu1Id+"_"+menu2Id;
					var btn = '';
					btn += "<input type='radio' id='"+radioName+"_0' name='"+radioName+"' value='0' checked='checked' />只读 ";  
					btn += "<input type='radio' id='"+radioName+"_1' name='"+radioName+"' value='1' />读写 ";
					btn += "<input type='radio' id='"+radioName+"_2' name='"+radioName+"' value='2' />机密";
					return btn;
				}
				return "";
			}
		}
		]] 
		});
	
	
	$("#roleName").combobox({
		onChange: function (newValue,oldValue) {
			clearTreeCheckbox();
			if(newValue != ''){
				
				$.ajax({
					type:"POST",
					url:'securityv2Ajax_queryMenus4RoleConfByRoleId.action',
					dataType:"json",
					data:{'roleId':newValue},
					success:function(msg){
						var jarray = jQuery.parseJSON(msg);
						for(var i=0;i<jarray.length;i++){
							var menu2Id4Tree = jarray[i].menu2Id4Tree;
							var accessTypeLevel = jarray[i].accessTypeLevel;
							$('#tt1').treegrid('checkNode',menu2Id4Tree);
							//$("#"+accessTypeLevel).select();
							jQuery("input[type='radio'][name='radio_"+menu2Id4Tree+"'][value='"+accessTypeLevel+"']").attr("checked", "checked");
						}
					},
					error:function(er){
						alert(er.responseText.trim());
						if(er.responseText!='' && er.responseText.indexOf('date error')!=-1){
							alert(er.responseText);
						}else{
							if(er.responseText!='' && er.responseText.indexOf('资源权限未分配')!=-1){
								alert('资源权限未分配');
							}
							if(er.responseText!='' && er.responseText.indexOf('登录')!=-1){
								alert('会话超时，请重新登录');
							}else{
								alert(er.responseText.trim());
							}
						}
					}
				});
	
			}
		}
	});
	
	$("#addRoleId").click(function(){
		getChecked();
		addRole();
	});
});

function clearTreeCheckbox(){
	var nodes = $('#tt1').treegrid('getCheckedNodes');//tree getChecked
	for(var i=0; i<nodes.length; i++){
		var node = nodes[i];
		var level = node.attributes.level;
		//if(level == 1){
		//	var menu1Id  = node.attributes.menu1Id;
		//	$('#tt1').treegrid('uncheckNode',menu1Id);
		//}
		if(level == 2){
			var menu1Id  = node.attributes.menu1Id;
			var menu2Id  = node.attributes.menu2Id;
			var radioName = "radio_"+menu1Id+"_"+menu2Id;
			var accessTypeLevel =  $("input[name='"+radioName+"']:checked").val();
			$('#tt1').treegrid('uncheckNode',menu1Id+"_"+menu2Id);
			jQuery("input[type='radio'][name='radio_"+menu1Id+"_"+menu2Id+"'][value='0']").attr("checked", "checked");
		}
	}
}

function addRole(){  
    //$("#addRoleId").val('处理中..');
    //$("#addRoleId").attr({"disabled":"disabled"});
    //document.formAddRole.submit();
    //$('#com').combobox('getValue')搜索获取当前选中的值
	//$('#com').combobox('getText')获取当前选中的文字
    $.ajax({
		type:"POST",
		url:'securityv2Ajax_addRole.action',
		dataType:"json",
		data:{'roleJsonArray':$("#roleJsonArray").val(),'roleName':$("#roleName").combobox('getText')},
		success:function(msg){
			alert(msg);
		},
		error:function(er){
			alert(er.responseText.trim());
			if(er.responseText!='' && er.responseText.indexOf('date error')!=-1){
				alert(er.responseText);
			}else{
				if(er.responseText!='' && er.responseText.indexOf('资源权限未分配')!=-1){
					alert('资源权限未分配');
				}
				if(er.responseText!='' && er.responseText.indexOf('登录')!=-1){
					alert('会话超时，请重新登录');
				}else{
					alert(er.responseText.trim());
				}
			}
		}
	});
}

function getChecked(){
	var nodes = $('#tt1').treegrid('getCheckedNodes');//tree getChecked
	var jsonArray = new Array();//创建一个数组
	for(var i=0; i<nodes.length; i++){
		var node = nodes[i];
		var level = node.attributes.level;
		if(level == 2){
			var menu1Id  = node.attributes.menu1Id;
			var menu2Id  = node.attributes.menu2Id;
			var radioName = "radio_"+menu1Id+"_"+menu2Id;
			var accessTypeLevel =  $("input[name='"+radioName+"']:checked").val();
			var json = {};
			json.menu1Id = menu1Id;
			json.menu2Id = menu2Id;
			json.accessTypeLevel = accessTypeLevel;
			jsonArray.push(json);
		}
	}
	$("#roleJsonArray").val(JSON.stringify(jsonArray));
}

</script>
</head>
<body class="font14">
<div style="float: left;">
<div style="color: red; text-align: center;">
	<s:property value="#attr.result"/>
</div>



<br/>
<s:form name="formAddRole" id="formAddRole" action="securityv2_addRole.action" namespace="/admin123/right_v2" theme="simple">
<table border="0" width="400"  bordercolor="#99BBE8" cellpadding="1" cellspacing="1">
	<caption>增加角色</caption>
	<tr>
		<td colspan="2">
			<input type="hidden" id="roleJsonArray" name="roleJsonArray" value="" >
		</td>
	</tr>
	<tr>
		<td>角色名称:</td>
		<td>
			<s:select 
			    id="roleName"
			    name="roleName"
			    list="#attr.roleDtoList" 
			    listKey="id" 
			    listValue="roleName"
			    headerKey=""
			    headerValue="请选择"
			    theme="simple"
			    cssStyle="width:200px;"
		    	cssClass="easyui-combobox"/>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="button" id="addRoleId" value="提交" >
		</td>
	</tr>
	<tr>
		<td colspan="2">
			注：提交的角色名称，系统中如果存在，则进行修改操作；不存在就进行添加操作。
		</td>
	</tr>
</table>
</s:form>
<br/>



</div>

<div style="float: right; margin-right: 50px;">
<table class="table2"  style="height: 600px;" cellpadding="0" cellspacing="0">
	<thead>
	<tr>
		<td>菜单树结构关系</td>
	</tr>
	</thead>
	<tr>
		<td>
			<div id="tt1" style="width: 600px;height: 600px; margin: 10px;" ></div>
		</td>
	</tr>
</table>
</div>


</body>
</html>